package cn.com.cym.gamezao.util;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.Base64;
import java.util.Random;

/**
 * 图片生成
 */
public class ImageUtils {

    private static Validate validate=null;

    private static int width=120;
    private static int height=40;
    private static int lineSize=40;

    private static Random random=new Random();

    private ImageUtils(){
        super();
    }

    private static Font getFont(){
        return new Font("Fixedsys",Font.CENTER_BASELINE,25);
    }

    private static Color getRandColor(int frontColor,int backColor){
        if(frontColor>255)
            frontColor=255;
        if(backColor>255)
            backColor=255;

        int red = frontColor+random.nextInt(backColor-frontColor-16);
        int greed = frontColor+random.nextInt(backColor-frontColor-14);
        int blue = frontColor+random.nextInt(backColor-frontColor-18);

        return new Color(red,greed,blue);
    }

    /**
     * 绘制字符串，返回绘制的字符串
     * @param g
     * @param randomString
     * @param i
     * @return
     */
    private static String drawString(Graphics g, String randomString, int i){
        Graphics2D g2d=(Graphics2D)g;
        g2d.setFont(getFont());
        g2d.setColor(new Color(random.nextFloat(),random.nextFloat(),random.nextFloat()));

        // String randChar=CommonUtils.random(6,2,4);

        // randomString+=randChar;

        int rot=getRandomNum(5,10);

        g2d.translate(random.nextInt(3),random.nextInt(3));
        g2d.rotate(rot*Math.PI/180);
        g2d.drawString(randomString,13*i,20);
        g2d.rotate(-rot*Math.PI/180);
        //int rot = RandomUtils
        return randomString;
    }

    private static int getRandomNum(int min,int max){
        int s = random.nextInt(max)%(max-min+1) + min;
        return s;
    }


    private static void drawLine(Graphics g){
        int x=random.nextInt(width);
        int y=random.nextInt(height);
        int xl=random.nextInt(13);
        int yl=random.nextInt(15);
        g.setColor(new Color(random.nextFloat(),random.nextFloat(),random.nextFloat()));
        g.drawLine(x,y,x+xl,y+yl);
    }

    public static Validate getRandomCode(){

        validate = validate==null ? new Validate():validate;

        BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR);

        Graphics g=image.getGraphics();

        g.fillRect(0,0,width,height);

        g.setFont(new Font("Times New Roman",Font.ROMAN_BASELINE,18));

        g.setColor(getRandColor(110,133));

        for(int i=0;i<=lineSize;i++){
            drawLine(g);
        }

        String randomString = CommonUtils.random(4,2,2);

      /*  cacheData.setValue(randomString);

        cacheData.setKey(userName);

        CommonUtils.CACHE_DATAS.add(cacheData);*/
      /*  for(int i=1;i<=5;i++){
            randomString=drawString(g,randomString,i);
            validate.setValue(randomString);
        }*/
        final char[] chars = randomString.toCharArray();
        for(int i=1;i<=chars.length;i++){
            drawString(g,chars[i-1]+"",i);
        }
        validate.setValue(randomString);

        g.dispose();//释放绘图资源

        ByteArrayOutputStream bs=null;

        try{
            bs = new ByteArrayOutputStream();

            ImageIO.write(image,"png",bs);

            String imgSrc = Base64.getEncoder().encodeToString(bs.toByteArray());

            validate.setBase64Str("data:image/png;base64,"+imgSrc);

        }catch (Exception i){

        }finally {
            try{
                bs.close();
            }catch (IOException e){
                e.printStackTrace();
            }finally {
                bs=null;
            }
        }

        return validate;
    }

    public static class Validate implements Serializable {

        private static final long serialVersionUID = -7466710425318584881L;

        private String base64Str;

        private String value;

        public String getBase64Str() {
            return base64Str;
        }

        public void setBase64Str(String base64Str) {
            this.base64Str = base64Str;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }

        @Override
        public String toString() {
            return "Validate{" +
                    "base64Str='" + base64Str + '\'' +
                    ", value='" + value + '\'' +
                    '}';
        }
    }

    /*public static void main(String[] args) {
        final Validate randomCode = ImageUtils.getRandomCode();
        System.out.println(randomCode);
       // System.out.println(CommonUtils.CACHE_DATAS);
    }*/
}
